home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / PG / DBPP20.ZIP / EXAMPLE2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-09  |  2.1 KB  |  82 lines

  1. /*
  2.    DataBase ++ 2.00.
  3.    
  4.    The following example creates a database and fills it with 10 records.
  5.    It creates 2 index tags and then displays all records on the screen
  6.    with both tag orders.
  7.    
  8.    Borland compile ( c4fox.lib == your codebase lib for FoxPro files ):
  9.  
  10.       bcc -ml -DS4FOX example2 dbpp200.lib c4fox.lib emu.lib mathl.lib cl.lib
  11. */
  12.  
  13. #include <dbppdata.hpp>             // DataBase++
  14. #include <iostream.h>
  15.  
  16. #define MAX_NAMES    10
  17. #define FNAME        "people.dbf"
  18.  
  19. void main()
  20. {
  21.    int i;
  22.    
  23.    // Create the DBDatabase object with the file name.
  24.    DBDatabase db( FNAME );
  25.  
  26.    // Create a DBStructure object and add fields to it.
  27.    DBStructure dbs;
  28.    dbs.addField( "name", 'C', 20 );
  29.    dbs.addField( "age", 'N', 3 );
  30.  
  31.    // Create a DBIndexTag object and fill it with tag info.
  32.    DBIndexTag idx;
  33.    idx.addTag( "NAME", "name" );
  34.    idx.addTag( "AGE", "age" );
  35.       
  36.    // Create the database and open it.
  37.    if ( ! db.create( dbs, idx ) )
  38.    {
  39.       cout << "Error creating file\n";
  40.       exit( 1 );
  41.    }
  42.    
  43.    // Create some names.
  44.    char *names[ MAX_NAMES ] = {
  45.       "Jeff", "Wendy", "Kyle", "Nicole", "Scott",
  46.       "Andy", "Dave", "Bruce", "Done", "Henry"
  47.    };
  48.  
  49.    // Open index files (dBASE only).
  50. //   db.openIndex( "name.ndx" );
  51. //   db.openIndex( "age.ndx" );
  52.    
  53.    // Add some records.
  54.    for ( i = 0; i < MAX_NAMES; i++ )
  55.    {
  56.       // Append a blank record.
  57.       db.append();
  58.  
  59.       // Replace a field with a string.
  60.       db.replace( "name", names[ i ] );
  61.  
  62.       // Replace a field with an int.
  63.       db.replace( "age", i * 10 );
  64.    }
  65.  
  66.    // Display all records with NAME set.
  67.    db.setIndexTag( "NAME" );
  68.    for ( db.goTop(); !db.eof(); db.skip() )
  69.       cout << db.getString( "name" ) << "   " << db.getInt( "age" ) << endl;
  70.  
  71.    cout << endl;
  72.    
  73.    // Display all records with AGE set.
  74.    db.setIndexTag( "AGE" );
  75.    for ( db.goTop(); !db.eof(); db.skip() )
  76.       cout << db.getString( "name" ) << "   " << db.getInt( "age" ) << endl;
  77.  
  78.    // Close file.
  79.    db.close();
  80. }
  81.  
  82.